package com.try01;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;

/**
 * Created by ztc on 15-10-31.
 */
public class MyFrame extends JFrame implements ActionListener,MouseListener,MouseWheelListener{
    JMenuBar jmb;
    JMenu jm1,jm2,jm3;
    JMenuItem[] jmi=new JMenuItem[7];
    JLabel jl,jl1;
    JPanel jp;
    JScrollPane jsp;
    ImageIcon imgIco,showii;
    File f;
    JFileChooser jfc;
    float width;
    float height;
    void init(){
        //创建对象
        //文件选择器
        jfc=new JFileChooser("./");
        //菜单栏
        jmb=new JMenuBar();
        jm1=new JMenu("文件");
        jm2=new JMenu("编辑");
        jm3=new JMenu("关于");
        jmi[0]=new JMenuItem("打开");
        jmi[0].addActionListener(this);
        jmi[1]=new JMenuItem("另存为");
        jmi[1].addActionListener(this);
        jmi[5]=new JMenuItem("上一张");
        jmi[5].addActionListener(this);
        jmi[6]=new JMenuItem("下一张");
        jmi[6].addActionListener(this);
        jmi[2]=new JMenuItem("放大");
        jmi[2].addActionListener(this);
        jmi[3]=new JMenuItem("缩小");
        jmi[3].addActionListener(this);
        jmi[4]=new JMenuItem("原图");
        jmi[4].addActionListener(this);
        jm1.add(jmi[0]);
        jm1.add(jmi[1]);
        jm1.add(jmi[5]);
        jm1.add(jmi[6]);
        jm2.add(jmi[2]);
        jm2.add(jmi[3]);
        jm2.add(jmi[4]);
        jmb.add(jm1);
        jmb.add(jm2);
        jmb.add(jm3);

        //显示区域
        jl=new JLabel("请选择图片。。。",JLabel.CENTER);
        jl.setForeground(Color.gray);
        jl.addMouseWheelListener(this);
        jl.addMouseListener(this);
        jsp=new JScrollPane(jl);

        //底部信息
        jl1=new JLabel("图片信息");
        //设置窗口


        this.setJMenuBar(jmb);
        this.add(jsp);
        this.add(jl1,"South");

        this.setSize(700,500);
        this.setLocation(300,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void ShowImg(){
        if(imgIco.getIconWidth()>695||imgIco.getIconHeight()>465){
            width=695;
            height=675*(float)imgIco.getIconHeight()/imgIco.getIconWidth();
            //System.out.println((float)ii.getIconHeight()/ii.getIconWidth());
        }else {
            width=imgIco.getIconWidth();
            height=imgIco.getIconHeight();
        }
        showii= new ImageIcon(
                imgIco.getImage().getScaledInstance((int)width,(int)height,0));
        this.jl.setText("");
        this.jl.setIcon(showii);
        ShowMsg();
    }
    public void Zoom(int flag){
        if((width<5000&&height<4000)&&(width>5&&height>4)) {
            if (flag == 0) {
                //放大
                width *= 1.3;height *= 1.3;
            } else if (flag == 1) {
                //缩小
                width *= 0.7;height *= 0.7;
            } else if (flag == 2) {
                //原图
                width = imgIco.getIconWidth();height = imgIco.getIconHeight();
            }
            showii = new ImageIcon(imgIco.getImage().getScaledInstance((int) width, (int) height, 0));
            this.jl.setIcon(showii);
            ShowMsg();
        }
        else if(width>5000||height>4000){
            width*=0.8;
            height*=0.8;
            JOptionPane.showMessageDialog(this, "再放大会失真！");
        }else if(width<5||height<3){
            width*=1.2;
            height*=1.2;
            JOptionPane.showMessageDialog(this, "再缩小就看不见了！");
        }
    }
    public void Save(){
        Image img=showii.getImage();
        try {
            JOptionPane.showMessageDialog(this,"以不同拓展名命名\r\n就以不同格式保存！(默认为gif)");
            //System.out.println(width+"  "+height+":"+img.getWidth(this)+"  "+img.getHeight(this));
            //获取类型
            BufferedImage choosed=ImageIO.read(f);
            BufferedImage bi=new BufferedImage((int)width,(int)height,choosed.getType());
            bi.getGraphics().drawImage(img, 0, 0, null);
            int result=jfc.showSaveDialog(this);
            File save=jfc.getSelectedFile();
            if(result==0&&save!=null) {
                String[] n=save.getName().split("\\.");
                //System.out.println(n[0]);
                ImageIO.write(bi,(n.length==1?"gif":n[1]),save);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void ShowMsg(){
        //设置底部信息
        String[] n=f.getName().split("\\.");
        float per=(float)showii.getIconWidth()/imgIco.getIconWidth();
        String msg="原图："+imgIco.getIconWidth()+"x"+imgIco.getIconHeight()+"像素。"
                +"显示："+showii.getIconWidth()+"x"+showii.getIconHeight()+"像素。"
                +"缩放度："+(int)(per*100)+"%"
                +"格式:"+(n.length==1?"未知":n[1]);
        this.jl1.setText(msg);
        this.setTitle(f.toString());
        this.jl1.setForeground(Color.gray);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jmi[0]||e.getSource()==jl){
            //设置上次打开位置
            jfc.setCurrentDirectory(jfc.getCurrentDirectory());
            //设置过滤
            jfc.setFileFilter(tools.myFilter());
            int result=jfc.showOpenDialog(this);
            f=jfc.getSelectedFile();
            if(result==0&&f!=null) {
                tools.addFile(f);
                new getFileList(jfc.getCurrentDirectory()).start();
                imgIco = new ImageIcon(f.getPath());
                ShowImg();
            }
        }else if(f!=null&&e.getSource()==jmi[2]){
            //放大
            Zoom(0);
        }else if(f!=null&&e.getSource()==jmi[3]){
            //缩小
            Zoom(1);
        }else if(f!=null&&e.getSource()==jmi[4]){
            //原图
            Zoom(2);
        }else if(f!=null&&e.getSource()==jmi[5]){
            //上一张
            f=tools.getLast(f);
            imgIco=new ImageIcon(f.getPath());
            ShowImg();
        }else if(f!=null&&e.getSource()==jmi[6]){
            //下一张
            f=tools.getNext(f);
            imgIco=new ImageIcon(f.getPath());
            ShowImg();
        }else if(f!=null&&e.getSource()==jmi[1]){
            //保存
            Save();
        }
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        //System.out.println(e.getWheelRotation()+"  "+e.getScrollAmount());
        if(f!=null) {
            if (e.getWheelRotation() == 1) {
                Zoom(0);
            } else if (e.getWheelRotation() == -1) {
                Zoom(1);
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(f!=null) {
            if (e.getClickCount() == 1 && e.getX() < 200) {
                f = tools.getLast(f);
                imgIco = new ImageIcon(f.getPath());
                ShowImg();
            } else if (e.getClickCount() == 1 && e.getX() > 500) {
                f = tools.getNext(f);
                imgIco = new ImageIcon(f.getPath());
                ShowImg();
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
